from typing import List, Union

from fastapi import Query
from pydantic import BaseModel, Field, RootModel
from typing_extensions import Literal

from forward.utils.tools import ToolsUtil


class ChannelOaInSchema(BaseModel):
    """
    公众号渠道参数
    """

    name: str = Field(default="", max_length=100)  # 小程序名称
    primary_id: str = Field(default="", max_length=100)  # 原始ID
    app_id: str = Field(default="", max_length=100)  # appId
    app_secret: str = Field(default="", max_length=200)  # appSecret
    qr_code: str = Field(default="", max_length=300)  # 小程序码
    url: str = Field(default="", max_length=300)  # URL
    token: str = Field(default="", max_length=200)  # Token
    encoding_aes_key: str = Field(default="", max_length=43)  # EncodingAESKey
    encryption_type: Literal[1, 2, 3] = Field(default=1)  # EncryptionType
    model_config = {
        "alias_generator": ToolsUtil.to_lower_camel,
        "populate_by_name": True,
    }


class ChannelOaSubMenusSchema(BaseModel):
    """
    公众号单项子菜单
    """

    name: str = Field(default="", max_length=100)  # 菜单名称
    # miniprogram=小程序, view=网页
    visit_type: str = Field(default="")
    url: str = Field(default="", max_length=100)  # 网址
    page_path: str = Field(default="")  # 路径
    app_id: str = Field(default="")  # 路径
    model_config = {
        "alias_generator": ToolsUtil.to_lower_camel,
        "populate_by_name": True,
    }


class ChannelOaMenusItemSchema(BaseModel):
    """
    公众号单项菜单
    """

    name: str = Field()  # 一级菜单名称
    menu_type: int = Field()  # 菜单类型 1=没有子菜单, 2=有子菜单
    # miniprogram=小程序, view=网页
    url: str
    app_id: str = Field(default="")
    page_path: str = Field(default="")  # 路径
    visit_type: str = Field(default="")
    sub_buttons: List[ChannelOaSubMenusSchema] | None = Field(default=None)
    model_config = {
        "alias_generator": ToolsUtil.to_lower_camel,
        "populate_by_name": True,
    }


class ChannelOaMenusInSchema(RootModel[list[ChannelOaMenusItemSchema]]):
    """
    公众号保存菜单参数
    """

    pass


class ChannelH5InSchema(BaseModel):
    """
    H5渠道参数
    """

    status: Literal[0, 1] = Field(alias="status")  # 是否关闭
    close: Literal[0, 1] = Field(alias="close")  # 关闭类型
    url: str = Field(default="", max_length=500)  # 关闭访问URL
    domain: str = Field()

class ChannelMpInSchema(BaseModel):
    """
    小程序渠道参数
    """

    name: str = Field(default="", max_length=100)  # 小程序名称
    primary_id: str = Field(default="", alias="primaryId", max_length=100)  # 原始ID
    app_id: str = Field(default="", alias="appId", max_length=100)  # appId
    app_secret: str = Field(default="", alias="appSecret", max_length=200)  # appSecret
    qr_code: str = Field(default="", alias="qrCode", max_length=300)  # 小程序码


class ChannelWxInSchema(BaseModel):
    """
    开放平台渠道参数
    """

    app_id: str = Field(default="", alias="appId", max_length=100)  # appId
    app_secret: str = Field(default="", alias="appSecret", max_length=200)  # appSecret


class ChannelOaReplyDefaultDetailInSchema(BaseModel):
    """渠道公众号默认回复详情参数"""

    id: int = Query(gt=0)  # 主键


class ChannelOaReplyDefaultCreateInSchema(BaseModel):
    """渠道公众号默认回复新增参数"""

    name: str  # 规则名称
    content: str  # 回复内容
    content_type: Literal[1, 2] = Field(alias="contentType")  # 内容类型
    matching_type: Union[Literal[1, 2], None] = Field(
        default=1, alias="matchingType"
    )  # 匹配方式
    status: Literal[0, 1]  # 状态


class ChannelOaReplyDefaultEditInSchema(ChannelOaReplyDefaultCreateInSchema):
    """渠道公众号默认回复编辑参数"""

    id: int = Field(gt=0)  # 主键


class ChannelOaReplyDefaultDelInSchema(BaseModel):
    """渠道公众号默认回复删除参数"""

    id: int = Field(gt=0)  # 主键


class ChannelOaReplyDefaultStatusInSchema(BaseModel):
    """渠道公众号默认回复状态参数"""

    id: int = Field(gt=0)  # 主键


class ChannelOaReplyFollowDetailInSchema(BaseModel):
    """渠道公众号关注回复详情参数"""

    id: int = Query(gt=0)  # 主键


class ChannelOaReplyFollowCreateInSchema(BaseModel):
    """渠道公众号关注回复新增参数"""

    name: str  # 规则名称
    content: str  # 回复内容
    content_type: Literal[1, 2] = Field(alias="contentType")  # 内容类型
    matching_type: Union[Literal[1, 2], None] = Field(
        default=1, alias="matchingType"
    )  # 匹配方式
    status: Literal[0, 1]  # 状态
    sort: int  # 排序编号


class ChannelOaReplyFollowEditInSchema(ChannelOaReplyFollowCreateInSchema):
    """渠道公众号关注回复编辑参数"""

    id: int = Field(gt=0)  # 主键


class ChannelOaReplyFollowDelInSchema(BaseModel):
    """渠道公众号关注回复删除参数"""

    id: int = Field(gt=0)  # 主键


class ChannelOaReplyFollowStatusInSchema(BaseModel):
    """渠道公众号关注回复状态参数"""

    id: int = Field(gt=0)  # 主键


class ChannelOaReplyKeywordDetailInSchema(BaseModel):
    """渠道公众号关键词回复详情参数"""

    id: int = Query(gt=0)  # 主键


class ChannelOaReplyKeywordCreateInSchema(BaseModel):
    """渠道公众号关键词回复新增参数"""

    name: str  # 规则名称
    content: str  # 回复内容
    keyword: str  # 关键词
    content_type: Literal[1, 2] = Field(alias="contentType")  # 内容类型
    matching_type: Union[Literal[1, 2], None] = Field(
        default=1, alias="matchingType"
    )  # 匹配方式
    status: Literal[0, 1]  # 状态
    sort: int = Field(default=50)  # 排序编号


class ChannelOaReplyKeywordEditInSchema(ChannelOaReplyKeywordCreateInSchema):
    """渠道公众号关键词回复编辑参数"""

    id: int = Field(gt=0)  # 主键


class ChannelOaReplyKeywordDelInSchema(BaseModel):
    """渠道公众号关键词回复删除参数"""

    id: int = Field(gt=0)  # 主键


class ChannelOaReplyKeywordStatusInSchema(BaseModel):
    """渠道公众号关键词回复状态参数"""

    id: int = Field(gt=0)  # 主键


class ChannelOaReplyOutSchema(BaseModel):
    """渠道公众号回复返回信息"""

    id: int  # 主键
    name: str  # 规则名称
    keyword: str  # 关键词
    content: str  # 回复内容
    reply_type: int = Field()  # 回复类型
    content_type: int = Field()  # 内容类型
    matching_type: int = Field()  # 匹配方式
    sort: int  # sort
    status: int  # 状态
    model_config = {
        "alias_generator": ToolsUtil.to_lower_camel,
        "populate_by_name": True,
    }
